<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8" />
    <title>Select - compareWith</title>
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <link href="../../../../../css/ionic.bundle.css" rel="stylesheet" />
    <link href="../../../../../scripts/testing/styles.css" rel="stylesheet" />
    <script src="../../../../../scripts/testing/scripts.js"></script>
    <script nomodule src="../../../../../dist/ionic/ionic.js"></script>
    <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
  </head>

  <body>
    <ion-app>
      <ion-header>
        <ion-toolbar>
          <ion-title>Select - compareWith</ion-title>
        </ion-toolbar>
      </ion-header>

      <ion-content>
        <ion-list>
          <ion-item>
            <ion-select label="Select multiple using objects and compareWith" id="multiple"></ion-select>
          </ion-item>
          <ion-item>
            <ion-select label="Select using objects and compareWith" id="single" multiple="true"></ion-select>
          </ion-item>
        </ion-list>
      </ion-content>

      <script>
        function compareWithFn(a, b) {
          return a.value === b.value;
        }

        let objectOptions = [
          {
            label: 'selected by default',
            value: '1',
          },
          {
            label: 'not selected by default',
            value: '2',
          },
        ];

        document.querySelectorAll('ion-select').forEach((select) => {
          select.compareWith = compareWithFn;

          objectOptions.forEach((option) => {
            let selectOption = document.createElement('ion-select-option');
            selectOption.value = option;
            selectOption.textContent = option.label;

            select.appendChild(selectOption);
          });
        });

        let objectSelectMultipleElement = document.getElementById('multiple');
        objectSelectMultipleElement.value = [objectOptions[0]];

        let objectSelectElement = document.getElementById('single');
        objectSelectElement.value = objectOptions[0];
      </script>
    </ion-app>
  </body>
</html>
